package bobochat.service.impl;

import bobochat.entity.FriendRequest;
import bobochat.entity.MyFriends;
import bobochat.entity.Users;
import bobochat.mapper.UsersMapper;
import bobochat.service.UsersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2019-11-08 22:25
 */
@Service
public class UsersServiceImpl implements UsersService {

    @Resource
    private UsersMapper usersMapper;

    @Override
    public List<Users> queryAllUsers() {
        List<Users> users = usersMapper.selectAllUsers();
        return users;
    }

    @Override
    public void register(Users users) {
        usersMapper.addUser(users);
    }

    @Override
    public Users login(Users users) {
        Users resultUser = usersMapper.selectUsersByUsernameAndPassword(users);
        return resultUser;
}
@Override
public Users queryUserByUsername(String username){
        Users users=usersMapper.selectUserByUsername(username);
        return users;
}
    @Override
    public Users updateNickname(Users users) {

        //更新昵称
        int rs = usersMapper.updateUsersNicknameByUsersid(users);
        if(rs <= 0){
            System.err.printf("昵称修改失败");
            return null;
        }else {

            //刷新用户
            Users user = usersMapper.selectUsersById(users.getId());
            return user;
        }


    }

    @Override
    public  Users updateFaceImage(Users users){

        int updateData=usersMapper.updateUsersFaceImageById(users);
        if(updateData <= 0){
            System.err.printf("头像修改失败");
            return null;
        }else {

            //刷新用户
            Users user = usersMapper.selectUsersById(users.getId());
            return user;
        }

    }
    @Override
    public Users updatePassword(Users users, String newPssword){
        int rp=usersMapper.updateUsersPasswordByUsersidAndPassword( newPssword, users.getId(), users.getPassword());
        if(rp <= 0){
            System.err.printf("密码修改失败");
            return null;
        }else {
          //刷新用户
            Users user = usersMapper.selectUsersById(users.getId());
            return user;
        }
    }


    @Override
    public void sendFriendRequest(FriendRequest friendRequest) {
        usersMapper.addFriendRequest(friendRequest);
    }

    @Override
    public void deleteFriendRequest(FriendRequest friendRequest){
        usersMapper.deleteRequestByid(friendRequest);
    }
    @Override
    public FriendRequest queryUserRequestById(String id){
        FriendRequest friendRequest= usersMapper.selsectFriendRuqestById(id);

        return friendRequest;
    }

    @Override
    public void addMyFriends(MyFriends myFriends){
        usersMapper.addFriends(myFriends);
    }

}
